package com.tjc.cloud.util.alipay

import com.alipay.api.AlipayClient
import com.alipay.api.AlipayConstants
import com.alipay.api.CertAlipayRequest
import com.alipay.api.DefaultAlipayClient
import com.alipay.api.request.AlipayFundTransUniTransferRequest
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration

@Configuration
@ComponentScan
class AutoConfiguration {
    @Bean
    @ConfigurationProperties("cloud.alipay")
    fun alipayProperties() = AlipayProperties()

    //    @Bean
//    fun alipayClient(alipayConfig: AlipayProperties): AlipayClient = DefaultAlipayClient(alipayConfig.serverUrl,
//        alipayConfig.appId,
//        alipayConfig.privateKey,
//        "json",
//        "utf8",
//        alipayConfig.publicKey,"RSA2")
    @Bean
    fun alipayClient(alipayConfig: AlipayProperties): AlipayClient {
        val certAlipayRequest = CertAlipayRequest()
        certAlipayRequest.serverUrl = alipayConfig.serverUrl
        certAlipayRequest.appId = alipayConfig.appId
        certAlipayRequest.privateKey = alipayConfig.privateKey
        certAlipayRequest.format = "JSON"
        certAlipayRequest.charset = "UTF-8"
        certAlipayRequest.signType = AlipayConstants.SIGN_TYPE_RSA2
        certAlipayRequest.certContent = alipayConfig.certContent
        certAlipayRequest.alipayPublicCertContent = alipayConfig.alipayPublicCertContent
        certAlipayRequest.rootCertContent = alipayConfig.rootCertContent
        return DefaultAlipayClient(certAlipayRequest)
    }


}
